<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
	content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<meta name="description" content="">
<meta http-equiv="x-dns-prefetch-control" content="on">

<title>注册</title>
	  <link href="../../css/css.css" rel="stylesheet" type="text/css">
	    <script src="../../js/jquery-v1.11.2.min.js"></script>
	    <script src="../../js/customer.js"></script>
	    <script src="../../js/common.js"></script>
</head>

<body>
	<form id="consignerForm" name="consignerForm">
		<section class="itemgroup p15 mt10">

			<div class="inputarea">
				<label class="inputname">手机号码</label> <label class="inputform">
					<i class="iconfont wipe">&#xe614;</i> <input class="formstyle w100"
					type="text" placeholder="请输入手机号码"  id="phone" name="phone"/>
				</label>
			</div>
			<div class="inputarea">
	            <label class="inputname">验证码</label>
	            <label class="inputformcode">
	                <img id="imgObj" alt="验证码" src="#"
	                     onclick="changeImg()"/>
	                <a href="#" onclick="changeImg()">换一张</a>
	            </label>
	        </div>
	        <div class="inputarea">
	            <label class="inputname">&nbsp;</label>
	            <label class="inputform">
	                <i class="iconfont wipe">&#xe614;</i>
	                <input class="formstyle w100" type="text" id="code" name="code" placeholder="请输入图片验证码"/>
	            </label>
	        </div>
			<div class="inputarea">
				<label class="inputname">验证码</label> <label class="inputformcode">
					<button  type="button" class="right btnkuang_bl checkcodebtn"  onclick="getValidateCode()" id="codeTips">获取验证码</button> <input
					class="formstyle w100" type="text" placeholder="请输入六位短信验证码"  id="smsCode" name="smsCode" onBlur="checkCode()"/>
				</label>
			</div>
			<div class="inputarea">
				<label class="inputname">密码</label> <label class="inputform">
					<i class="iconfont wipe">&#xe614;</i> <input class="formstyle w100"
					type="password" placeholder="6-12位密码"  id="password" name="password"/>
				</label>
			</div>
			<div class="inputarea">
				<label class="inputname">确认密码</label> <label class="inputform">
					<i class="iconfont wipe">&#xe614;</i> <input class="formstyle w100"
					type="password" placeholder="再次确认密码" id="repassword" name="repassword"/>
				</label>
			</div>
			<div class="inputarea">
				<label class="inputname">推荐人</label> <label class="inputform">
					<i class="iconfont wipe">&#xe614;</i> <input class="formstyle w100"
					type="text" placeholder="请输入推荐人手机号码" id="recommendPhone" name="recommendPhone"/>
				</label>
			</div>
		</section>
		<section class="signbtnarea">
			<div class="photoitem font12">
				<label for="gcheckbox" class="gray font14 mr5"> <input
					type="checkbox" class="checkbox-g" id="gcheckbox" checked="checked">
					<i class="iconfont checkbox-icon"></i>
				</label> 已阅读并同意<font class="orange">《用户协议》</font>
			</div>
			<button type="button" class="btn bottomonebtn mt10" onclick="save()">提交</button>
		</section>
	</form>
</body>
<script>
var uuid = 0;
$(document).ready(function () {
    var url = _global.serverHost + "/before_validateCode.htm";
    var data = null;
    _callAjaxRequest(url, data, ajaxBack);
    function ajaxBack(data) {
        uuid = data.uuid;
        $("#imgObj").attr("src", _global.serverHost + "/validateCode.htm?code=" + data.verifyCode + "&uuid=" + uuid);
    }
});

	function save() {
		var phone = $("#phone").val();
        if (phone == null || phone == '' || isNaN(phone)) {
            alert("请输入手机号码");
            return false;
        }
		
       /*  var smsCode = $("#smsCode").val();
        if (smsCode == null || smsCode == '') {
            alert("请输入短信验证码");
            return false;
        } */
        var password = $("#password").val();
        if (password == null || password == '') {
            alert("请输入密码");
            return false;
        }
        if (password.length < 6 || password.length > 12) {
            alert("请输入6-12位密码");
            return false;
        }
        var repassword = $("#repassword").val();
        if (repassword == null || repassword == '') {
            alert("请输入确认密码");
            return false;
        }
        if (repassword != password) {
            alert("两次输入密码不一致");
            return false;
        }
        var recommendPhone = $("#recommendPhone").val();
        if (recommendPhone != null && recommendPhone != '' && isNaN(recommendPhone)) {
            alert("推荐人手机号码输入有误");
            return false;
        }
		var url =  _global.serverHost + "/consignerRegisterAndLogin/registerSave.htm";
		$.ajax({
			type : "POST",
			url : url,
			data : $('#consignerForm').serialize(),
			contentType : "application/x-www-form-urlencoded;charset=UTF-8",
			success : function(data) {
				alert(data.outResultReason);
				if (data.outResult == "1") {
					setCookie('token',data.token);
					//成功，请求登录页面
					window.location.href= _global.serverHost + "/html/customer/index.html";
				}
			}
		});

	}
	
	 function checkCode() {
	        var phone = $("#phone").val();
	        var smsCode = $("#smsCode").val();

	        console.log(phone + "==" + smsCode);
	        if (phone == null || phone == '' || smsCode == null || smsCode == '') {
	            return false;
	        }
	        $.ajax({
	            type: 'POST',
	            url: _global.serverHost + '/smsVerification.htm',
	            dataType: "json",
	            data: {phone: phone, code: smsCode},
	            success: function (data) {
	                if (data.code != '1') {
	                	alert('短信验证码不正确');
	                    //$("#errorMessage").show();
	                    $("#next").attr("disabled", true);
	                } else {
	                    //$("#errorMessage").hide();
	                    $("#next").removeAttr("disabled");
	                }
	            }
	        });
	    }

	    function getValidateCode() {
	        var phone = $("#phone").val();
	        if (phone == null || phone == '' || isNaN(phone)) {
	            alert("请输入手机号码");
	            return false;
	        }
	        var code = $("#code").val();
	        if (code == null || code == '') {
	            alert("请输入图片验证码");
	            return false;
	        }
	        $.ajax({
	            type: 'POST',
	            url: _global.serverHost + '/verification.htm',
	            dataType: "json",
	            data: {phone: phone, code: code, uuid: uuid},
	            success: function (data) {
	                alert(data.message);
	                if (data.code == '1') {
	                    settime();
	                } else {
	                    changeImg();
	                }
	            }
	        });
	    }
	    // 刷新图片
	    function changeImg() {
	        var imgSrc = $("#imgObj");
	        var src = imgSrc.attr("src");
	        imgSrc.attr("src", changeUrl(src));
	    }

	    //为了使每次生成图片不一致，即不让浏览器读缓存，所以需要加上时间戳
	    function changeUrl(url) {
	        var timestamp = (new Date()).valueOf();
	        var index = url.indexOf("?", url);
	        if (index > 0) {
	            url = url.substring(0, index);
	        }
	        if ((url.indexOf("&") >= 0)) {
	            url = url + "×tamp=" + timestamp;
	        } else {
	            url = url + "?timestamp=" + timestamp;
	        }
	        return url + "&uuid=" + uuid;
	    }

	    var countdown = 60;

	    function settime() {
	        var obj = $("#codeTips");
	        if (countdown == 0) {
	            obj.removeAttr("disabled");
	            obj.html("免费获取验证码");
	            countdown = 60;
	            return;
	        } else {
	            obj.attr("disabled", true);
	            obj.html("重新发送(" + countdown + ")");
	            countdown--;
	        }
	        setTimeout(function () {
	                    settime(obj)
	                }
	                , 1000)
	    }
</script>
</html>
